package com.flying.carclub.activity;

import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Toast;

import com.flying.carclub.BaseActivity;
import com.flying.carclub.R;
import com.flying.carclub.api.ApiImpl;
import com.flying.carclub.bean.UserInfoBean;
import com.flying.carclub.http.OnResponseListener;
import com.flying.carclub.utils.SPUtils;
import com.flying.carclub.widget.InfoBar;

public class MyInfoActivity extends BaseActivity implements View.OnClickListener {
    private static int REQUEST_CODE_EDIT_NAME = 0x001;
    private static int REQUEST_CODE_EDIT_NICKNAME = 0x010;


    private InfoBar phoneBar;
    private InfoBar nameBar;
    private InfoBar nickBar;
    private InfoBar sexBar;
    private UserInfoBean userInfoBean;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_info);
        initView();
        refreshView();
    }

    private void initView() {

        phoneBar = findViewById(R.id.bar_phone);
        nameBar = findViewById(R.id.bar_name);
        nickBar = findViewById(R.id.bar_nickname);
        sexBar = findViewById(R.id.bar_sex);
        phoneBar.hideRightArraw();
        nameBar.setOnClickListener(this);
        nickBar.setOnClickListener(this);
        sexBar.setOnClickListener(this);

    }

    private void refreshView() {
        userInfoBean = SPUtils.getUserInfo(MyInfoActivity.this);
        if (userInfoBean != null) {
            phoneBar.setValueName(userInfoBean.username);
            nameBar.setValueName(userInfoBean.realName);
            nickBar.setValueName(userInfoBean.nickname);
            sexBar.setValueName(userInfoBean.sex == 0 ? "男" : "女");
        }
    }

    @Override
    public void onClick(View v) {
        int viewId = v.getId();
        switch (viewId) {
            case R.id.bar_name:
                Intent intent = new Intent(MyInfoActivity.this, EditInfoActivity.class);
                intent.putExtra(EditInfoActivity.EXTRA_TYPE,EditInfoActivity.TYPE_NAME);
                startActivityForResult(intent, REQUEST_CODE_EDIT_NAME);
                break;
            case R.id.bar_nickname:
                Intent intent1 = new Intent(MyInfoActivity.this, EditInfoActivity.class);
                intent1.putExtra(EditInfoActivity.EXTRA_TYPE,EditInfoActivity.TYPE_NICKNAME);
                startActivityForResult(intent1, REQUEST_CODE_EDIT_NICKNAME);
                break;
            case R.id.bar_sex:
                final String[] sexs = {"男", "女"};
                AlertDialog.Builder builder = new AlertDialog.Builder(MyInfoActivity.this).setItems(sexs, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, final int which) {

                        showLoadingDialog();
                        String username = userInfoBean != null ? userInfoBean.username : "";
                        new ApiImpl().httpUpdateUserInfo(username, "sex", which + "", new OnResponseListener() {
                            @Override
                            public void onSuccess(Object response) {
                                dismissLoadingDialog();
                                UserInfoBean userInfoBean = (UserInfoBean) response;
                                if (userInfoBean != null) {
                                    SPUtils.setUserInfo(MyInfoActivity.this, userInfoBean);
                                }
                                sexBar.setValueName(sexs[which]);
                                showToast("更新成功");
                            }

                            @Override
                            public void onError(String err) {
                                dismissLoadingDialog();
                                if (TextUtils.isEmpty(err)) {
                                    err = "网络请求失败";
                                }
                                showToast(err);
                            }
                        });
                    }
                });
                builder.show();
                break;

        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            refreshView();
        }
    }
}
